<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>deferred.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_third_party_closure_goog_mochikit_async_deferred.js.html">deferred.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 Bob Ippolito. All Rights Reserved.
<a name="line2"></a>// Modifications Copyright 2009 The Closure Library Authors. All Rights
<a name="line3"></a>// Reserved.
<a name="line4"></a>
<a name="line5"></a>/**
<a name="line6"></a> * @license Portions of this code are from MochiKit, received by
<a name="line7"></a> * The Closure Authors under the MIT license. All other code is Copyright
<a name="line8"></a> * 2005-2009 The Closure Authors. All Rights Reserved.
<a name="line9"></a> */
<a name="line10"></a>
<a name="line11"></a>/**
<a name="line12"></a> * @fileoverview Classes for tracking asynchronous operations and handling the
<a name="line13"></a> * results. The Deferred object here is patterned after the Deferred object in
<a name="line14"></a> * the Twisted python networking framework.
<a name="line15"></a> *
<a name="line16"></a> * See: http://twistedmatrix.com/projects/core/documentation/howto/defer.html
<a name="line17"></a> *
<a name="line18"></a> * Based on the Dojo code which in turn is based on the MochiKit code.
<a name="line19"></a> *
<a name="line20"></a> */
<a name="line21"></a>
<a name="line22"></a>goog.provide(&#39;goog.async.Deferred&#39;);
<a name="line23"></a>goog.provide(&#39;goog.async.Deferred.AlreadyCalledError&#39;);
<a name="line24"></a>goog.provide(&#39;goog.async.Deferred.CancelledError&#39;);
<a name="line25"></a>
<a name="line26"></a>goog.require(&#39;goog.array&#39;);
<a name="line27"></a>goog.require(&#39;goog.asserts&#39;);
<a name="line28"></a>goog.require(&#39;goog.debug.Error&#39;);
<a name="line29"></a>
<a name="line30"></a>
<a name="line31"></a>
<a name="line32"></a>/**
<a name="line33"></a> * A Deferred represents the result of an asynchronous operation. A Deferred
<a name="line34"></a> * instance has no result when it is created, and is &quot;fired&quot; (given an initial
<a name="line35"></a> * result) by calling {@code callback} or {@code errback}.
<a name="line36"></a> *
<a name="line37"></a> * Once fired, the result is passed through a sequence of callback functions
<a name="line38"></a> * registered with {@code addCallback} or {@code addErrback}. The functions may
<a name="line39"></a> * mutate the result before it is passed to the next function in the sequence.
<a name="line40"></a> *
<a name="line41"></a> * Callbacks and errbacks may be added at any time, including after the Deferred
<a name="line42"></a> * has been &quot;fired&quot;. If there are no pending actions in the execution sequence
<a name="line43"></a> * of a fired Deferred, any new callback functions will be called with the last
<a name="line44"></a> * computed result. Adding a callback function is the only way to access the
<a name="line45"></a> * result of the Deferred.
<a name="line46"></a> *
<a name="line47"></a> * If a Deferred operation is cancelled, an optional user-provided cancellation
<a name="line48"></a> * function is invoked which may perform any special cleanup, followed by firing
<a name="line49"></a> * the Deferred&#39;s errback sequence with a {@code CancelledError}. If the
<a name="line50"></a> * Deferred has already fired, cancellation is ignored.
<a name="line51"></a> *
<a name="line52"></a> * @param {Function=} opt_onCancelFunction A function that will be called if the
<a name="line53"></a> *     Deferred is cancelled. If provided, this function runs before the
<a name="line54"></a> *     Deferred is fired with a {@code CancelledError}.
<a name="line55"></a> * @param {Object=} opt_defaultScope The default object context to call
<a name="line56"></a> *     callbacks and errbacks in.
<a name="line57"></a> * @constructor
<a name="line58"></a> */
<a name="line59"></a>goog.async.Deferred = function(opt_onCancelFunction, opt_defaultScope) {
<a name="line60"></a>  /**
<a name="line61"></a>   * Entries in the sequence are arrays containing a callback, an errback, and
<a name="line62"></a>   * an optional scope. The callback or errback in an entry may be null.
<a name="line63"></a>   * @type {!Array.&lt;!Array&gt;}
<a name="line64"></a>   * @private
<a name="line65"></a>   */
<a name="line66"></a>  this.sequence_ = [];
<a name="line67"></a>
<a name="line68"></a>  /**
<a name="line69"></a>   * Optional function that will be called if the Deferred is cancelled.
<a name="line70"></a>   * @type {Function|undefined}
<a name="line71"></a>   * @private
<a name="line72"></a>   */
<a name="line73"></a>  this.onCancelFunction_ = opt_onCancelFunction;
<a name="line74"></a>
<a name="line75"></a>  /**
<a name="line76"></a>   * The default scope to execute callbacks and errbacks in.
<a name="line77"></a>   * @type {Object}
<a name="line78"></a>   * @private
<a name="line79"></a>   */
<a name="line80"></a>  this.defaultScope_ = opt_defaultScope || null;
<a name="line81"></a>};
<a name="line82"></a>
<a name="line83"></a>
<a name="line84"></a>/**
<a name="line85"></a> * Whether the Deferred has been fired.
<a name="line86"></a> * @type {boolean}
<a name="line87"></a> * @private
<a name="line88"></a> */
<a name="line89"></a>goog.async.Deferred.prototype.fired_ = false;
<a name="line90"></a>
<a name="line91"></a>
<a name="line92"></a>/**
<a name="line93"></a> * Whether the last result in the execution sequence was an error.
<a name="line94"></a> * @type {boolean}
<a name="line95"></a> * @private
<a name="line96"></a> */
<a name="line97"></a>goog.async.Deferred.prototype.hadError_ = false;
<a name="line98"></a>
<a name="line99"></a>
<a name="line100"></a>/**
<a name="line101"></a> * The current Deferred result, updated as callbacks and errbacks are executed.
<a name="line102"></a> * @type {*}
<a name="line103"></a> * @private
<a name="line104"></a> */
<a name="line105"></a>goog.async.Deferred.prototype.result_;
<a name="line106"></a>
<a name="line107"></a>
<a name="line108"></a>/**
<a name="line109"></a> * Whether the Deferred is blocked waiting on another Deferred to fire. If a
<a name="line110"></a> * callback or errback returns a Deferred as a result, the execution sequence is
<a name="line111"></a> * blocked until that Deferred result becomes available.
<a name="line112"></a> * @type {boolean}
<a name="line113"></a> * @private
<a name="line114"></a> */
<a name="line115"></a>goog.async.Deferred.prototype.blocked_ = false;
<a name="line116"></a>
<a name="line117"></a>
<a name="line118"></a>/**
<a name="line119"></a> * Whether this Deferred is blocking execution of another Deferred. If this
<a name="line120"></a> * instance was returned as a result in another Deferred&#39;s execution sequence,
<a name="line121"></a> * that other Deferred becomes blocked until this instance&#39;s execution sequence
<a name="line122"></a> * completes. No additional callbacks may be added to a Deferred once it
<a name="line123"></a> * is blocking another instance.
<a name="line124"></a> * @type {boolean}
<a name="line125"></a> * @private
<a name="line126"></a> */
<a name="line127"></a>goog.async.Deferred.prototype.blocking_ = false;
<a name="line128"></a>
<a name="line129"></a>
<a name="line130"></a>/**
<a name="line131"></a> * Whether the Deferred has been cancelled without having a custom cancel
<a name="line132"></a> * function.
<a name="line133"></a> * @type {boolean}
<a name="line134"></a> * @private
<a name="line135"></a> */
<a name="line136"></a>goog.async.Deferred.prototype.silentlyCancelled_ = false;
<a name="line137"></a>
<a name="line138"></a>
<a name="line139"></a>/**
<a name="line140"></a> * If an error is thrown during Deferred execution with no errback to catch it,
<a name="line141"></a> * the error is rethrown after a timeout. Reporting the error after a timeout
<a name="line142"></a> * allows execution to continue in the calling context.
<a name="line143"></a> * @type {number}
<a name="line144"></a> * @private
<a name="line145"></a> */
<a name="line146"></a>goog.async.Deferred.prototype.unhandledExceptionTimeoutId_;
<a name="line147"></a>
<a name="line148"></a>
<a name="line149"></a>/**
<a name="line150"></a> * If this Deferred was created by branch(), this will be the &quot;parent&quot; Deferred.
<a name="line151"></a> * @type {goog.async.Deferred}
<a name="line152"></a> * @private
<a name="line153"></a> */
<a name="line154"></a>goog.async.Deferred.prototype.parent_;
<a name="line155"></a>
<a name="line156"></a>
<a name="line157"></a>/**
<a name="line158"></a> * The number of Deferred objects that have been branched off this one. This
<a name="line159"></a> * will be decremented whenever a branch is fired or cancelled.
<a name="line160"></a> * @type {number}
<a name="line161"></a> * @private
<a name="line162"></a> */
<a name="line163"></a>goog.async.Deferred.prototype.branches_ = 0;
<a name="line164"></a>
<a name="line165"></a>
<a name="line166"></a>/**
<a name="line167"></a> * Cancels a Deferred that has not yet been fired, or is blocked on another
<a name="line168"></a> * deferred operation. If this Deferred is waiting for a blocking Deferred to
<a name="line169"></a> * fire, the blocking Deferred will also be cancelled.
<a name="line170"></a> *
<a name="line171"></a> * If this Deferred was created by calling branch() on a parent Deferred with
<a name="line172"></a> * opt_propagateCancel set to true, the parent may also be cancelled. If
<a name="line173"></a> * opt_deepCancel is set, cancel() will be called on the parent (as well as any
<a name="line174"></a> * other ancestors if the parent is also a branch). If one or more branches were
<a name="line175"></a> * created with opt_propagateCancel set to true, the parent will be cancelled if
<a name="line176"></a> * cancel() is called on all of those branches.
<a name="line177"></a> *
<a name="line178"></a> * @param {boolean=} opt_deepCancel If true, cancels this Deferred&#39;s parent even
<a name="line179"></a> *     if cancel() hasn&#39;t been called on some of the parent&#39;s branches. Has no
<a name="line180"></a> *     effect on a branch without opt_propagateCancel set to true.
<a name="line181"></a> */
<a name="line182"></a>goog.async.Deferred.prototype.cancel = function(opt_deepCancel) {
<a name="line183"></a>  if (!this.hasFired()) {
<a name="line184"></a>    if (this.parent_) {
<a name="line185"></a>      // Get rid of the parent reference before potentially running the parent&#39;s
<a name="line186"></a>      // canceller function to ensure that this cancellation isn&#39;t
<a name="line187"></a>      // double-counted.
<a name="line188"></a>      var parent = this.parent_;
<a name="line189"></a>      delete this.parent_;
<a name="line190"></a>      if (opt_deepCancel) {
<a name="line191"></a>        parent.cancel(opt_deepCancel);
<a name="line192"></a>      } else {
<a name="line193"></a>        parent.branchCancel_();
<a name="line194"></a>      }
<a name="line195"></a>    }
<a name="line196"></a>
<a name="line197"></a>    if (this.onCancelFunction_) {
<a name="line198"></a>      // Call in user-specified scope.
<a name="line199"></a>      this.onCancelFunction_.call(this.defaultScope_, this);
<a name="line200"></a>    } else {
<a name="line201"></a>      this.silentlyCancelled_ = true;
<a name="line202"></a>    }
<a name="line203"></a>    if (!this.hasFired()) {
<a name="line204"></a>      this.errback(new goog.async.Deferred.CancelledError(this));
<a name="line205"></a>    }
<a name="line206"></a>  } else if (this.result_ instanceof goog.async.Deferred) {
<a name="line207"></a>    this.result_.cancel();
<a name="line208"></a>  }
<a name="line209"></a>};
<a name="line210"></a>
<a name="line211"></a>
<a name="line212"></a>/**
<a name="line213"></a> * Handle a single branch being cancelled. Once all branches are cancelled, this
<a name="line214"></a> * Deferred will be cancelled as well.
<a name="line215"></a> *
<a name="line216"></a> * @private
<a name="line217"></a> */
<a name="line218"></a>goog.async.Deferred.prototype.branchCancel_ = function() {
<a name="line219"></a>  this.branches_--;
<a name="line220"></a>  if (this.branches_ &lt;= 0) {
<a name="line221"></a>    this.cancel();
<a name="line222"></a>  }
<a name="line223"></a>};
<a name="line224"></a>
<a name="line225"></a>
<a name="line226"></a>/**
<a name="line227"></a> * Called after a blocking Deferred fires. Unblocks this Deferred and resumes
<a name="line228"></a> * its execution sequence.
<a name="line229"></a> *
<a name="line230"></a> * @param {boolean} isSuccess Whether the result is a success or an error.
<a name="line231"></a> * @param {*} res The result of the blocking Deferred.
<a name="line232"></a> * @private
<a name="line233"></a> */
<a name="line234"></a>goog.async.Deferred.prototype.continue_ = function(isSuccess, res) {
<a name="line235"></a>  this.blocked_ = false;
<a name="line236"></a>  this.updateResult_(isSuccess, res);
<a name="line237"></a>};
<a name="line238"></a>
<a name="line239"></a>
<a name="line240"></a>/**
<a name="line241"></a> * Updates the current result based on the success or failure of the last action
<a name="line242"></a> * in the execution sequence.
<a name="line243"></a> *
<a name="line244"></a> * @param {boolean} isSuccess Whether the new result is a success or an error.
<a name="line245"></a> * @param {*} res The result.
<a name="line246"></a> * @private
<a name="line247"></a> */
<a name="line248"></a>goog.async.Deferred.prototype.updateResult_ = function(isSuccess, res) {
<a name="line249"></a>  this.fired_ = true;
<a name="line250"></a>  this.result_ = res;
<a name="line251"></a>  this.hadError_ = !isSuccess;
<a name="line252"></a>  this.fire_();
<a name="line253"></a>};
<a name="line254"></a>
<a name="line255"></a>
<a name="line256"></a>/**
<a name="line257"></a> * Verifies that the Deferred has not yet been fired.
<a name="line258"></a> *
<a name="line259"></a> * @private
<a name="line260"></a> * @throws {Error} If this has already been fired.
<a name="line261"></a> */
<a name="line262"></a>goog.async.Deferred.prototype.check_ = function() {
<a name="line263"></a>  if (this.hasFired()) {
<a name="line264"></a>    if (!this.silentlyCancelled_) {
<a name="line265"></a>      throw new goog.async.Deferred.AlreadyCalledError(this);
<a name="line266"></a>    }
<a name="line267"></a>    this.silentlyCancelled_ = false;
<a name="line268"></a>  }
<a name="line269"></a>};
<a name="line270"></a>
<a name="line271"></a>
<a name="line272"></a>/**
<a name="line273"></a> * Fire the execution sequence for this Deferred by passing the starting result
<a name="line274"></a> * to the first registered callback.
<a name="line275"></a> * @param {*=} opt_result The starting result.
<a name="line276"></a> */
<a name="line277"></a>goog.async.Deferred.prototype.callback = function(opt_result) {
<a name="line278"></a>  this.check_();
<a name="line279"></a>  this.assertNotDeferred_(opt_result);
<a name="line280"></a>  this.updateResult_(true /* isSuccess */, opt_result);
<a name="line281"></a>};
<a name="line282"></a>
<a name="line283"></a>
<a name="line284"></a>/**
<a name="line285"></a> * Fire the execution sequence for this Deferred by passing the starting error
<a name="line286"></a> * result to the first registered errback.
<a name="line287"></a> * @param {*=} opt_result The starting error.
<a name="line288"></a> */
<a name="line289"></a>goog.async.Deferred.prototype.errback = function(opt_result) {
<a name="line290"></a>  this.check_();
<a name="line291"></a>  this.assertNotDeferred_(opt_result);
<a name="line292"></a>  this.updateResult_(false /* isSuccess */, opt_result);
<a name="line293"></a>};
<a name="line294"></a>
<a name="line295"></a>
<a name="line296"></a>/**
<a name="line297"></a> * Asserts that an object is not a Deferred.
<a name="line298"></a> * @param {*} obj The object to test.
<a name="line299"></a> * @throws {Error} Throws an exception if the object is a Deferred.
<a name="line300"></a> * @private
<a name="line301"></a> */
<a name="line302"></a>goog.async.Deferred.prototype.assertNotDeferred_ = function(obj) {
<a name="line303"></a>  goog.asserts.assert(
<a name="line304"></a>      !(obj instanceof goog.async.Deferred),
<a name="line305"></a>      &#39;An execution sequence may not be initiated with a blocking Deferred.&#39;);
<a name="line306"></a>};
<a name="line307"></a>
<a name="line308"></a>
<a name="line309"></a>/**
<a name="line310"></a> * Register a callback function to be called with a successful result. If no
<a name="line311"></a> * value is returned by the callback function, the result value is unchanged. If
<a name="line312"></a> * a new value is returned, it becomes the Deferred result and will be passed to
<a name="line313"></a> * the next callback in the execution sequence.
<a name="line314"></a> *
<a name="line315"></a> * If the function throws an error, the error becomes the new result and will be
<a name="line316"></a> * passed to the next errback in the execution chain.
<a name="line317"></a> *
<a name="line318"></a> * If the function returns a Deferred, the execution sequence will be blocked
<a name="line319"></a> * until that Deferred fires. Its result will be passed to the next callback (or
<a name="line320"></a> * errback if it is an error result) in this Deferred&#39;s execution sequence.
<a name="line321"></a> *
<a name="line322"></a> * @param {!function(this:T,?):?} cb The function to be called with a successful
<a name="line323"></a> *     result.
<a name="line324"></a> * @param {T=} opt_scope An optional scope to call the callback in.
<a name="line325"></a> * @return {!goog.async.Deferred} This Deferred.
<a name="line326"></a> * @template T
<a name="line327"></a> */
<a name="line328"></a>goog.async.Deferred.prototype.addCallback = function(cb, opt_scope) {
<a name="line329"></a>  return this.addCallbacks(cb, null, opt_scope);
<a name="line330"></a>};
<a name="line331"></a>
<a name="line332"></a>
<a name="line333"></a>/**
<a name="line334"></a> * Register a callback function to be called with an error result. If no value
<a name="line335"></a> * is returned by the function, the error result is unchanged. If a new error
<a name="line336"></a> * value is returned or thrown, that error becomes the Deferred result and will
<a name="line337"></a> * be passed to the next errback in the execution sequence.
<a name="line338"></a> *
<a name="line339"></a> * If the errback function handles the error by returning a non-error value,
<a name="line340"></a> * that result will be passed to the next normal callback in the sequence.
<a name="line341"></a> *
<a name="line342"></a> * If the function returns a Deferred, the execution sequence will be blocked
<a name="line343"></a> * until that Deferred fires. Its result will be passed to the next callback (or
<a name="line344"></a> * errback if it is an error result) in this Deferred&#39;s execution sequence.
<a name="line345"></a> *
<a name="line346"></a> * @param {!function(this:T,?):?} eb The function to be called on an
<a name="line347"></a> *     unsuccessful result.
<a name="line348"></a> * @param {T=} opt_scope An optional scope to call the errback in.
<a name="line349"></a> * @return {!goog.async.Deferred} This Deferred.
<a name="line350"></a> * @template T
<a name="line351"></a> */
<a name="line352"></a>goog.async.Deferred.prototype.addErrback = function(eb, opt_scope) {
<a name="line353"></a>  return this.addCallbacks(null, eb, opt_scope);
<a name="line354"></a>};
<a name="line355"></a>
<a name="line356"></a>
<a name="line357"></a>/**
<a name="line358"></a> * Registers one function as both a callback and errback.
<a name="line359"></a> *
<a name="line360"></a> * @param {!function(this:T,?):?} f The function to be called on any result.
<a name="line361"></a> * @param {T=} opt_scope An optional scope to call the function in.
<a name="line362"></a> * @return {!goog.async.Deferred} This Deferred.
<a name="line363"></a> * @template T
<a name="line364"></a> */
<a name="line365"></a>goog.async.Deferred.prototype.addBoth = function(f, opt_scope) {
<a name="line366"></a>  return this.addCallbacks(f, f, opt_scope);
<a name="line367"></a>};
<a name="line368"></a>
<a name="line369"></a>
<a name="line370"></a>/**
<a name="line371"></a> * Registers a callback function and an errback function at the same position
<a name="line372"></a> * in the execution sequence. Only one of these functions will execute,
<a name="line373"></a> * depending on the error state during the execution sequence.
<a name="line374"></a> *
<a name="line375"></a> * NOTE: This is not equivalent to {@code def.addCallback().addErrback()}! If
<a name="line376"></a> * the callback is invoked, the errback will be skipped, and vice versa.
<a name="line377"></a> *
<a name="line378"></a> * @param {(function(this:T,?):?)|null} cb The function to be called on a
<a name="line379"></a> *     successful result.
<a name="line380"></a> * @param {(function(this:T,?):?)|null} eb The function to be called on an
<a name="line381"></a> *     unsuccessful result.
<a name="line382"></a> * @param {T=} opt_scope An optional scope to call the functions in.
<a name="line383"></a> * @return {!goog.async.Deferred} This Deferred.
<a name="line384"></a> * @template T
<a name="line385"></a> */
<a name="line386"></a>goog.async.Deferred.prototype.addCallbacks = function(cb, eb, opt_scope) {
<a name="line387"></a>  goog.asserts.assert(!this.blocking_, &#39;Blocking Deferreds can not be re-used&#39;);
<a name="line388"></a>  this.sequence_.push([cb, eb, opt_scope]);
<a name="line389"></a>  if (this.hasFired()) {
<a name="line390"></a>    this.fire_();
<a name="line391"></a>  }
<a name="line392"></a>  return this;
<a name="line393"></a>};
<a name="line394"></a>
<a name="line395"></a>
<a name="line396"></a>/**
<a name="line397"></a> * Links another Deferred to the end of this Deferred&#39;s execution sequence. The
<a name="line398"></a> * result of this execution sequence will be passed as the starting result for
<a name="line399"></a> * the chained Deferred, invoking either its first callback or errback.
<a name="line400"></a> *
<a name="line401"></a> * @param {!goog.async.Deferred} otherDeferred The Deferred to chain.
<a name="line402"></a> * @return {!goog.async.Deferred} This Deferred.
<a name="line403"></a> */
<a name="line404"></a>goog.async.Deferred.prototype.chainDeferred = function(otherDeferred) {
<a name="line405"></a>  this.addCallbacks(
<a name="line406"></a>      otherDeferred.callback, otherDeferred.errback, otherDeferred);
<a name="line407"></a>  return this;
<a name="line408"></a>};
<a name="line409"></a>
<a name="line410"></a>
<a name="line411"></a>/**
<a name="line412"></a> * Makes this Deferred wait for another Deferred&#39;s execution sequence to
<a name="line413"></a> * complete before continuing.
<a name="line414"></a> *
<a name="line415"></a> * This is equivalent to adding a callback that returns {@code otherDeferred},
<a name="line416"></a> * but doesn&#39;t prevent additional callbacks from being added to
<a name="line417"></a> * {@code otherDeferred}.
<a name="line418"></a> *
<a name="line419"></a> * @param {!goog.async.Deferred} otherDeferred The Deferred to wait for.
<a name="line420"></a> * @return {!goog.async.Deferred} This Deferred.
<a name="line421"></a> */
<a name="line422"></a>goog.async.Deferred.prototype.awaitDeferred = function(otherDeferred) {
<a name="line423"></a>  return this.addCallback(goog.bind(otherDeferred.branch, otherDeferred));
<a name="line424"></a>};
<a name="line425"></a>
<a name="line426"></a>
<a name="line427"></a>/**
<a name="line428"></a> * Creates a branch off this Deferred&#39;s execution sequence, and returns it as a
<a name="line429"></a> * new Deferred. The branched Deferred&#39;s starting result will be shared with the
<a name="line430"></a> * parent at the point of the branch, even if further callbacks are added to the
<a name="line431"></a> * parent.
<a name="line432"></a> *
<a name="line433"></a> * All branches at the same stage in the execution sequence will receive the
<a name="line434"></a> * same starting value.
<a name="line435"></a> *
<a name="line436"></a> * @param {boolean=} opt_propagateCancel If cancel() is called on every child
<a name="line437"></a> *     branch created with opt_propagateCancel, the parent will be cancelled as
<a name="line438"></a> *     well.
<a name="line439"></a> * @return {!goog.async.Deferred} A Deferred that will be started with the
<a name="line440"></a> *     computed result from this stage in the execution sequence.
<a name="line441"></a> */
<a name="line442"></a>goog.async.Deferred.prototype.branch = function(opt_propagateCancel) {
<a name="line443"></a>  var d = new goog.async.Deferred();
<a name="line444"></a>  this.chainDeferred(d);
<a name="line445"></a>  if (opt_propagateCancel) {
<a name="line446"></a>    d.parent_ = this;
<a name="line447"></a>    this.branches_++;
<a name="line448"></a>  }
<a name="line449"></a>  return d;
<a name="line450"></a>};
<a name="line451"></a>
<a name="line452"></a>
<a name="line453"></a>/**
<a name="line454"></a> * @return {boolean} Whether the execution sequence has been started on this
<a name="line455"></a> *     Deferred by invoking {@code callback} or {@code errback}.
<a name="line456"></a> */
<a name="line457"></a>goog.async.Deferred.prototype.hasFired = function() {
<a name="line458"></a>  return this.fired_;
<a name="line459"></a>};
<a name="line460"></a>
<a name="line461"></a>
<a name="line462"></a>/**
<a name="line463"></a> * @param {*} res The latest result in the execution sequence.
<a name="line464"></a> * @return {boolean} Whether the current result is an error that should cause
<a name="line465"></a> *     the next errback to fire. May be overridden by subclasses to handle
<a name="line466"></a> *     special error types.
<a name="line467"></a> * @protected
<a name="line468"></a> */
<a name="line469"></a>goog.async.Deferred.prototype.isError = function(res) {
<a name="line470"></a>  return res instanceof Error;
<a name="line471"></a>};
<a name="line472"></a>
<a name="line473"></a>
<a name="line474"></a>/**
<a name="line475"></a> * @return {boolean} Whether an errback exists in the remaining sequence.
<a name="line476"></a> * @private
<a name="line477"></a> */
<a name="line478"></a>goog.async.Deferred.prototype.hasErrback_ = function() {
<a name="line479"></a>  return goog.array.some(this.sequence_, function(sequenceRow) {
<a name="line480"></a>    // The errback is the second element in the array.
<a name="line481"></a>    return goog.isFunction(sequenceRow[1]);
<a name="line482"></a>  });
<a name="line483"></a>};
<a name="line484"></a>
<a name="line485"></a>
<a name="line486"></a>/**
<a name="line487"></a> * Exhausts the execution sequence while a result is available. The result may
<a name="line488"></a> * be modified by callbacks or errbacks, and execution will block if the
<a name="line489"></a> * returned result is an incomplete Deferred.
<a name="line490"></a> *
<a name="line491"></a> * @private
<a name="line492"></a> */
<a name="line493"></a>goog.async.Deferred.prototype.fire_ = function() {
<a name="line494"></a>  if (this.unhandledExceptionTimeoutId_ &amp;&amp; this.hasFired() &amp;&amp;
<a name="line495"></a>      this.hasErrback_()) {
<a name="line496"></a>    // It is possible to add errbacks after the Deferred has fired. If a new
<a name="line497"></a>    // errback is added immediately after the Deferred encountered an unhandled
<a name="line498"></a>    // error, but before that error is rethrown, cancel the rethrow.
<a name="line499"></a>    goog.global.clearTimeout(this.unhandledExceptionTimeoutId_);
<a name="line500"></a>    delete this.unhandledExceptionTimeoutId_;
<a name="line501"></a>  }
<a name="line502"></a>
<a name="line503"></a>  if (this.parent_) {
<a name="line504"></a>    this.parent_.branches_--;
<a name="line505"></a>    delete this.parent_;
<a name="line506"></a>  }
<a name="line507"></a>
<a name="line508"></a>  var res = this.result_;
<a name="line509"></a>  var unhandledException = false;
<a name="line510"></a>  var isNewlyBlocked = false;
<a name="line511"></a>
<a name="line512"></a>  while (this.sequence_.length &amp;&amp; !this.blocked_) {
<a name="line513"></a>    var sequenceEntry = this.sequence_.shift();
<a name="line514"></a>
<a name="line515"></a>    var callback = sequenceEntry[0];
<a name="line516"></a>    var errback = sequenceEntry[1];
<a name="line517"></a>    var scope = sequenceEntry[2];
<a name="line518"></a>
<a name="line519"></a>    var f = this.hadError_ ? errback : callback;
<a name="line520"></a>    if (f) {
<a name="line521"></a>      /** @preserveTry */
<a name="line522"></a>      try {
<a name="line523"></a>        var ret = f.call(scope || this.defaultScope_, res);
<a name="line524"></a>
<a name="line525"></a>        // If no result, then use previous result.
<a name="line526"></a>        if (goog.isDef(ret)) {
<a name="line527"></a>          // Bubble up the error as long as the return value hasn&#39;t changed.
<a name="line528"></a>          this.hadError_ = this.hadError_ &amp;&amp; (ret == res || this.isError(ret));
<a name="line529"></a>          this.result_ = res = ret;
<a name="line530"></a>        }
<a name="line531"></a>
<a name="line532"></a>        if (res instanceof goog.async.Deferred) {
<a name="line533"></a>          isNewlyBlocked = true;
<a name="line534"></a>          this.blocked_ = true;
<a name="line535"></a>        }
<a name="line536"></a>
<a name="line537"></a>      } catch (ex) {
<a name="line538"></a>        res = ex;
<a name="line539"></a>        this.hadError_ = true;
<a name="line540"></a>
<a name="line541"></a>        if (!this.hasErrback_()) {
<a name="line542"></a>          // If an error is thrown with no additional errbacks in the queue,
<a name="line543"></a>          // prepare to rethrow the error.
<a name="line544"></a>          unhandledException = true;
<a name="line545"></a>        }
<a name="line546"></a>      }
<a name="line547"></a>    }
<a name="line548"></a>  }
<a name="line549"></a>
<a name="line550"></a>  this.result_ = res;
<a name="line551"></a>
<a name="line552"></a>  if (isNewlyBlocked) {
<a name="line553"></a>    res.addCallbacks(
<a name="line554"></a>        goog.bind(this.continue_, this, true /* isSuccess */),
<a name="line555"></a>        goog.bind(this.continue_, this, false /* isSuccess */));
<a name="line556"></a>    res.blocking_ = true;
<a name="line557"></a>  }
<a name="line558"></a>
<a name="line559"></a>  if (unhandledException) {
<a name="line560"></a>    // Rethrow the unhandled error after a timeout. Execution will continue, but
<a name="line561"></a>    // the error will be seen by global handlers and the user. The throw will
<a name="line562"></a>    // be canceled if another errback is appended before the timeout executes.
<a name="line563"></a>    // The error&#39;s original stack trace is preserved where available.
<a name="line564"></a>    this.unhandledExceptionTimeoutId_ = goog.global.setTimeout(function() {
<a name="line565"></a>      throw res;
<a name="line566"></a>    }, 0);
<a name="line567"></a>  }
<a name="line568"></a>};
<a name="line569"></a>
<a name="line570"></a>
<a name="line571"></a>/**
<a name="line572"></a> * Creates a Deferred that has an initial result.
<a name="line573"></a> *
<a name="line574"></a> * @param {*=} opt_result The result.
<a name="line575"></a> * @return {!goog.async.Deferred} The new Deferred.
<a name="line576"></a> */
<a name="line577"></a>goog.async.Deferred.succeed = function(opt_result) {
<a name="line578"></a>  var d = new goog.async.Deferred();
<a name="line579"></a>  d.callback(opt_result);
<a name="line580"></a>  return d;
<a name="line581"></a>};
<a name="line582"></a>
<a name="line583"></a>
<a name="line584"></a>/**
<a name="line585"></a> * Creates a Deferred that has an initial error result.
<a name="line586"></a> *
<a name="line587"></a> * @param {*} res The error result.
<a name="line588"></a> * @return {!goog.async.Deferred} The new Deferred.
<a name="line589"></a> */
<a name="line590"></a>goog.async.Deferred.fail = function(res) {
<a name="line591"></a>  var d = new goog.async.Deferred();
<a name="line592"></a>  d.errback(res);
<a name="line593"></a>  return d;
<a name="line594"></a>};
<a name="line595"></a>
<a name="line596"></a>
<a name="line597"></a>/**
<a name="line598"></a> * Creates a Deferred that has already been cancelled.
<a name="line599"></a> *
<a name="line600"></a> * @return {!goog.async.Deferred} The new Deferred.
<a name="line601"></a> */
<a name="line602"></a>goog.async.Deferred.cancelled = function() {
<a name="line603"></a>  var d = new goog.async.Deferred();
<a name="line604"></a>  d.cancel();
<a name="line605"></a>  return d;
<a name="line606"></a>};
<a name="line607"></a>
<a name="line608"></a>
<a name="line609"></a>/**
<a name="line610"></a> * Normalizes values that may or may not be Deferreds.
<a name="line611"></a> *
<a name="line612"></a> * If the input value is a Deferred, the Deferred is branched (so the original
<a name="line613"></a> * execution sequence is not modified) and the input callback added to the new
<a name="line614"></a> * branch. The branch is returned to the caller.
<a name="line615"></a> *
<a name="line616"></a> * If the input value is not a Deferred, the callback will be executed
<a name="line617"></a> * immediately and an already firing Deferred will be returned to the caller.
<a name="line618"></a> *
<a name="line619"></a> * In the following (contrived) example, if &lt;code&gt;isImmediate&lt;/code&gt; is true
<a name="line620"></a> * then 3 is alerted immediately, otherwise 6 is alerted after a 2-second delay.
<a name="line621"></a> *
<a name="line622"></a> * &lt;pre&gt;
<a name="line623"></a> * var value;
<a name="line624"></a> * if (isImmediate) {
<a name="line625"></a> *   value = 3;
<a name="line626"></a> * } else {
<a name="line627"></a> *   value = new goog.async.Deferred();
<a name="line628"></a> *   setTimeout(function() { value.callback(6); }, 2000);
<a name="line629"></a> * }
<a name="line630"></a> *
<a name="line631"></a> * var d = goog.async.Deferred.when(value, alert);
<a name="line632"></a> * &lt;/pre&gt;
<a name="line633"></a> *
<a name="line634"></a> * @param {*} value Deferred or normal value to pass to the callback.
<a name="line635"></a> * @param {!function(this:T, ?):?} callback The callback to execute.
<a name="line636"></a> * @param {T=} opt_scope An optional scope to call the callback in.
<a name="line637"></a> * @return {!goog.async.Deferred} A new Deferred that will call the input
<a name="line638"></a> *     callback with the input value.
<a name="line639"></a> * @template T
<a name="line640"></a> */
<a name="line641"></a>goog.async.Deferred.when = function(value, callback, opt_scope) {
<a name="line642"></a>  if (value instanceof goog.async.Deferred) {
<a name="line643"></a>    return value.branch(true).addCallback(callback, opt_scope);
<a name="line644"></a>  } else {
<a name="line645"></a>    return goog.async.Deferred.succeed(value).addCallback(callback, opt_scope);
<a name="line646"></a>  }
<a name="line647"></a>};
<a name="line648"></a>
<a name="line649"></a>
<a name="line650"></a>
<a name="line651"></a>/**
<a name="line652"></a> * An error sub class that is used when a Deferred has already been called.
<a name="line653"></a> * @param {!goog.async.Deferred} deferred The Deferred.
<a name="line654"></a> *
<a name="line655"></a> * @constructor
<a name="line656"></a> * @extends {goog.debug.Error}
<a name="line657"></a> */
<a name="line658"></a>goog.async.Deferred.AlreadyCalledError = function(deferred) {
<a name="line659"></a>  goog.debug.Error.call(this);
<a name="line660"></a>
<a name="line661"></a>  /**
<a name="line662"></a>   * The Deferred that raised this error.
<a name="line663"></a>   * @type {goog.async.Deferred}
<a name="line664"></a>   */
<a name="line665"></a>  this.deferred = deferred;
<a name="line666"></a>};
<a name="line667"></a>goog.inherits(goog.async.Deferred.AlreadyCalledError, goog.debug.Error);
<a name="line668"></a>
<a name="line669"></a>
<a name="line670"></a>/** @override */
<a name="line671"></a>goog.async.Deferred.AlreadyCalledError.prototype.message =
<a name="line672"></a>    &#39;Deferred has already fired&#39;;
<a name="line673"></a>
<a name="line674"></a>
<a name="line675"></a>/** @override */
<a name="line676"></a>goog.async.Deferred.AlreadyCalledError.prototype.name = &#39;AlreadyCalledError&#39;;
<a name="line677"></a>
<a name="line678"></a>
<a name="line679"></a>
<a name="line680"></a>/**
<a name="line681"></a> * An error sub class that is used when a Deferred is cancelled.
<a name="line682"></a> * TODO(brenneman): Cancelled -&gt; American English Canceled.
<a name="line683"></a> *
<a name="line684"></a> * @param {!goog.async.Deferred} deferred The Deferred object.
<a name="line685"></a> * @constructor
<a name="line686"></a> * @extends {goog.debug.Error}
<a name="line687"></a> */
<a name="line688"></a>goog.async.Deferred.CancelledError = function(deferred) {
<a name="line689"></a>  goog.debug.Error.call(this);
<a name="line690"></a>
<a name="line691"></a>  /**
<a name="line692"></a>   * The Deferred that raised this error.
<a name="line693"></a>   * @type {goog.async.Deferred}
<a name="line694"></a>   */
<a name="line695"></a>  this.deferred = deferred;
<a name="line696"></a>};
<a name="line697"></a>goog.inherits(goog.async.Deferred.CancelledError, goog.debug.Error);
<a name="line698"></a>
<a name="line699"></a>
<a name="line700"></a>/** @override */
<a name="line701"></a>goog.async.Deferred.CancelledError.prototype.message = &#39;Deferred was cancelled&#39;;
<a name="line702"></a>
<a name="line703"></a>
<a name="line704"></a>/** @override */
<a name="line705"></a>goog.async.Deferred.CancelledError.prototype.name = &#39;CancelledError&#39;;
</pre>


</body>
</html>
